package com.wgj.android.accesslibility;

import android.app.Application;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

import com.jiaai.xui.utils.XAppUtils;
import com.jiaai.xui.utils.XPreferencesUtils;
import com.wgj.android.WXFriendsAccessibilityService;

/**
 * Created by Administrator on 2017/11/1.
 */

public class AccessibilityUtil  {

    private   static  final String IS_OPEN_WEIXI="isopenweixi";
    private  static  final String IS_AUTO_DEL="isAutoDel";
    private  static  final  String IS_OPEN_VIBRATE="isOpenVibrate";
    private  static  final  String IS_OPEN_SOUND ="isOpneSound";
    private  static  final  String  SELLP_TIME="sellpTime";
    /**
     *检查清粉服务是否开启
     * @param mContext
     * @return
     */
    public static boolean checkWXFriendsAccessibilityServiceIsOpen(Context mContext) {
        int accessibilityEnabled = 0;
        final String service = mContext.getPackageName() + "/" + WXFriendsAccessibilityService.class.getCanonicalName();

        try {
            accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED);

        } catch (Settings.SettingNotFoundException e) {

        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1) {

            String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
         if (settingValue != null) {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();


                    if (accessibilityService.equalsIgnoreCase(service)) {
                        return true;
                    }
                }
            }
        } else {

        }
        return false;
    }

    /**
     *检查服务是否开启
     * @param mContext
     * @return
     */
    public static boolean checkAccessibilityServiceIsOpen(Context mContext,Class clzz) {
        int accessibilityEnabled = 0;
        final String service = mContext.getPackageName() + "/" + clzz.getCanonicalName();

        try {
            accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED);

        } catch (Settings.SettingNotFoundException e) {

        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1) {

            String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext()) {
                    String accessibilityService = mStringColonSplitter.next();


                    if (accessibilityService.equalsIgnoreCase(service)) {
                        return true;
                    }
                }
            }
        } else {

        }
        return false;
    }

    public static  boolean  isOpenWeiXi(){
        return (Boolean) XPreferencesUtils.get(IS_OPEN_WEIXI,true);
    }

    public  static  void setOpenXeiXi(boolean isOpen){
        XPreferencesUtils.put(IS_OPEN_WEIXI,isOpen);
    }

    public  static  void openWxiXi(Application application){
            if(XAppUtils.isInstallApp("com.tencent.mm")){
                XAppUtils.startApp("com.tencent.mm");

            }
    }

    /**
     * 检查是否开启自动删除僵尸粉
     * @return
     */
    public  static  boolean isAutoDel(){
        return (Boolean) XPreferencesUtils.get(IS_AUTO_DEL,false);
    }


    public  static  void setIsAutoDel(boolean isOpen){
        XPreferencesUtils.put(IS_AUTO_DEL,isOpen);
    }

    /**
     * 检查是否开启震动
     * @return
     */
    public  static  boolean isOpenVibraie(){
        return (Boolean) XPreferencesUtils.get(IS_OPEN_VIBRATE,false);
    }

    public  static void setIsOpenVibrate(boolean isOpen){
        XPreferencesUtils.put(IS_OPEN_VIBRATE,isOpen);
    }

    /**
     * 检查是否开启声音提示
     * @return
     */
    public  static  boolean isOpenSound(){
        return (Boolean) XPreferencesUtils.get(IS_OPEN_SOUND,true);
    }
    public  static void setIsOpenSound(boolean isOpenSound){
        XPreferencesUtils.put(IS_OPEN_SOUND,isOpenSound);
    }


    public  static  int getSleepTime(){
        return (int) XPreferencesUtils.get(SELLP_TIME,1);
    }

    public  static void  setSellpTime(int time){
        XPreferencesUtils.put(SELLP_TIME,time);
    }
}
